function [optList] = getDASRegistryOptions(varargin)
% GETDASREGISTRYOPTIONS extract the list of search options in DAS Registry
% 
%GETDASREGISTRYOPTIONS extract the list of search options in DAS Registry
%that will populated SEARCHREGISTRY_GUI menus with search parameters
% 
% SYNTAX: [optList] = getDASRegistryOptions(varargin)
%          optList - cellstr list with the options according to the
%          criteria
%
% EXAMPLES: 
%             Get organisms' list
%             [optList] = getDASRegistryOptions() or
%             [optList] = getDASRegistryOptions('criteria','organism')
%             
%             Get coordinate sytems' list
%             [optList] = getDASRegistryOptions('criteria','type')
%             
%             Get authorities' list
%             [optList] = getDASRegistryOptions('criteria','authority')
%             
%             Get capabilities' list
%             [optList] = getDASRegistryOptions('criteria','capability')
%
% See also SEARCHREGISTRY_GUI
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.

p = inputParser;   % Create an instance of the class.

p.addParamValue('timeout', 90000, @isnumeric); %time in miliseconds
p.addParamValue('criteria', 'organism', @(x) isValidCriteria(x));

p.KeepUnmatched = true;
p.FunctionName = 'GETDASREGISTRYOPTIONS';
p.parse(varargin{:});

switch p.Results.criteria

    case 'type'
        optList = parseDASXMLbyType(p.Results.timeout);
    case 'organism'
        optList = parseDASXMLbyOrganism(p.Results.timeout);
    case 'authority'
        optList = parseDASXMLbyAuthority(p.Results.timeout);
    case 'capability'
        optList = parseDASXMLbyCapability(p.Results.timeout);

end

function [typesList] = parseDASXMLbyType(timeout)

[xmlStruct, xml, status] = readDASURL('http://www.dasregistry.org/das1/coordinatesystem',timeout);

if (~status)
    %error('Server is taking too long to respond. Increase timeout and try again.');
    optList = -1;
    return;
end

if (~isstruct(xmlStruct))
%     error('Problem parsing XML response.');
    optList = -1;
    return;
end

typesList = {};
for i=1:size(xmlStruct.COORDINATES,2)
   
    list = regexp(xmlStruct.COORDINATES{i}.CONTENT,',','split');
    
    for j=1:size(list,2)
        
        if (~any(strcmpi(list{j},typesList)))
            typesList = [typesList; list{j}];
        end
    end
end
typesList = sort(typesList);

function [organismList] = parseDASXMLbyOrganism(timeout)

[xmlStruct, xml, status] = readDASURL('http://www.dasregistry.org/das1/organism',timeout);

if (~status)
%     error('Server is taking too long to respond. Increase timeout and try again.');
    optList = -1;
    return;
end

if (~isstruct(xmlStruct))
%     error('Problem parsing XML response.');
    optList = -1;
    return;
end

organismList = {};

for i=1:size(xmlStruct.ORGANISM,2)
    
    org = xmlStruct.ORGANISM{i}.ATTRIBUTE.scientificName;

    if (~any(strcmpi(org,organismList)))
        organismList = [organismList; org];
    end
    
end
organismList = sort(organismList);

function [authList] = parseDASXMLbyAuthority(timeout)

[xmlStruct, xml, status] = readDASURL('http://www.dasregistry.org/das1/sources/',timeout);

if (~status)
%     error('Server is taking too long to respond. Increase timeout and try again.');
    optList = -1;
    return;
end

if (~isstruct(xmlStruct))
%     error('Problem parsing XML response.');
    optList = -1;
    return;
end

authList = {};
for i=1:size(xmlStruct.SOURCE,2)
    
   if (isfield(xmlStruct.SOURCE{i}.VERSION{1},'COORDINATES'))
       
       for k=1:size(xmlStruct.SOURCE{i}.VERSION{1}.COORDINATES,2)

           auth = xmlStruct.SOURCE{i}.VERSION{1}.COORDINATES{k}.ATTRIBUTE.authority;

           if (~any(strcmpi(auth,authList)))
               authList = [authList; auth];
           end
       end
   end
end

authList = sort(authList);

function [capList] = parseDASXMLbyCapability(timeout)

[xmlStruct, xml, status] = readDASURL('http://www.dasregistry.org/das1/sources/',timeout);

if (~status)
%     error('Server is taking too long to respond. Increase timeout and try again.');
    optList = -1;
    return;
end

if (~isstruct(xmlStruct))
%     error('Problem parsing XML response.');
    optList = -1;
    return;
end

capList = {};
for i=1:size(xmlStruct.SOURCE,2)
    
   if (isfield(xmlStruct.SOURCE{i}.VERSION{1},'CAPABILITY'))
       
       for k=1:size(xmlStruct.SOURCE{i}.VERSION{1}.CAPABILITY,2)

           capTokens = regexp(xmlStruct.SOURCE{i}.VERSION{1}.CAPABILITY{k}.ATTRIBUTE.type,':','split');
           
           if (~any(strcmpi(capTokens{2},capList)))
               capList = [capList; capTokens{2}];
           end
       end
   end
end
capList = sort(capList);

function [res] = isValidCriteria(x)

res = any(strcmpi(x,{'type','organism','authority','capability'}));